<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 
      1. 先实现一个方法，用于在一个数组中，找 属于 p1 数组的子数组
        [{ id: "p1-1", pid: "p1", title: "广州" }]
          属于 p2 数组的 children
        [{ id: "p2-1", pid: "p2", title: "成都" },{ id: "p2-2", pid: "p2", title: "德阳" },{ id: "p2-3", pid: "p2", title: "绵阳" },]

      

     -->

    <script>
      const listData = [
        { id: "p1", title: "广东" },
        { id: "p2", title: "四川" },
        { id: "p2-1", pid: "p2", title: "成都" },
        { id: "p1-1", pid: "p1", title: "广州" },
        { id: "p2-2", pid: "p2", title: "德阳" },
        { id: "p2-3", pid: "p2", title: "绵阳" },
        { id: "p2-1-1", pid: "p2-1", title: "高新区" },
      ];

      function getChildren(list, pid) {
        return list
          .filter((item) => item.pid === pid)
          .map((item) => {
            const curItem = { ...item };
            const children = getChildren(list, item.id);
            if (children.length) {
              curItem.children = children;
            }
            return curItem;
          });
      }

      const arr1 = getChildren(listData);
      // console.log(arr1);

      /**
       * 求数组 arr 中，大于 max 的和
       * @param {number[]} arr
       * @param {number} max
       */
      function sum(arr, max) {
        return arr
          .filter((item) => item > max)
          .reduce((result, item) => {
            return (result += item);
          }, 0);
      }

      console.log(sum([1, 2, 3, 4, 6], 5)); // 6
      console.log(sum([1, 2, 3, 4, 6], 1)); // 15

      function getMonthDays(year, month) {
        // return new Date(year, month, 0).getDate();

        const time = new Date(year, month).getTime() - 24 * 60 * 60 * 1000;
        return new Date(time).getDate();
      }

      console.log(getMonthDays(2023, 5)); // 31
      console.log(getMonthDays(2023, 2)); // 28
      console.log(getMonthDays(2024, 2)); // 29

      // 推荐两款前端操作日期时间的 库
      // moment.js day.js
      // 推荐两款前端处理数值类型的 库，解决 精度计算问题
      // Math.js、BigDecimal.js
    </script>
  </body>
</html>
